<template>
  <div class="form-container">
    <div class="form-top"></div>
    <div class="form-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/userLevelList' }">等级列表</el-breadcrumb-item>
        <el-breadcrumb-item>添加等级</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="form-content">

      <el-form ref="form" :model="form" :rules="rules" label-width="150px">

        <el-form-item label="用户等级" prop="level">
          <el-col :span="9">
            <el-input v-model="form.level" placeholder="请输入等级"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="等级所需经验下限" prop="min">
          <el-col :span="9">
            <el-input v-model="form.min" placeholder="请输入等级所需经验下限"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="等级所需经验上限" prop="max">
          <el-col :span="9">
            <el-input v-model="form.max" placeholder="请输入等级所需经验上限"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="用户头衔" prop="rank">
          <el-col :span="9">
            <el-input v-model="form.rank" placeholder="用户头衔"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-row>

            <el-button type="danger" @click="confirmAdd('form')">立即保存</el-button>
            <el-button type="default" @click="cancel()">返回</el-button>
          </el-row>
        </el-form-item>

      </el-form>
    </div>
  </div>

</template>

<script>

  export default {
    data() {
      return {
        validate: false,
        form: {
          level:'',
          min:'',
          max:'',
          rank: '',
        },
        rules: {
          level: [
            {required: true, message: '请输入用户等级', trigger: 'blur'},
          ],
          min: [
            {required: true, message: '请输入用户等级所需经验下限', trigger: 'blur'},
          ],
          max: [
            {required: true, message: '请输入用户等级所需经验上限', trigger: 'blur'},
          ],
          rank: [
            {required: true, message: '请输入用户头衔', trigger: 'blur'},
          ],
        },
        filter_keyword: '',

      }
    },
    mounted() {
    },
    methods: {
      confirmAdd(form) {
        this.validateFormData(form);
        if (this.validate) {
          const _ths = this;

          this.$http.post('/api/root/UserLevel/setList', {...this.form})
            .then(rest => {
              if (rest.msg == 'ok') {
                this.$message.success('新增成功');
                setTimeout(function () {
                  _ths.$router.push({name: 'userLevelList'})
                }, 800)
              } else {
                this.form = {
                  ...this.form,
                };
                this.$message.error(rest.msg);
              }
            })
        }
      },
      cancel() {
        this.$router.push({name: 'userLevelList'})
      },

      /**
       * 验证表单数据
       * @param form
       */
      validateFormData(form) {
        this.$refs[form].validate((valid) => {
          if (!valid) {
            this.$message({
              message: '表单数据未填写完整,操作失败！',
              type: 'warning'
            });
            return false;
          }
          this.validate = true;
          return true;
        })
      },
    }
  }
</script>
<style lang="less" scoped>
  @import "index.less";
</style>